import time

import requests
import re
"""
获取数据：
response.text:获取响应文本数据 --> 字符串
response.json():获取响应json数据 --> 字典/列表
responsi.content: --> 获取响应的二进制数据
"""
# https://m.baode11.cc/ob/19028/
url = "https://m.baode11.cc/ob/"
# 书的id
book_id = "22867_1/"
# 内容id
content_id = "7524051"

# 写文件
def write_file(content, title):
    file_path = "C:/Users/Administrator/Desktop/python测试创建文件夹/小说下载/"
    with open(f"{file_path}/{title}.txt", "w", encoding="utf-8") as file:
        file.write(content)
    print(title, "下载完毕")
# 获取响应内容
def get_html(url):
    resp = requests.get(url)
    resp.encoding = "utf-8"
    return resp.text
# 获取第二页内容id
def get_next_text(html):
    pattern = '<a class="dise" href="//m.baode11.cc/ob/19028/(\d+_\d+).html">下一页</a>'
    return "".join(re.findall(pattern, html))
# 获得小说内容
def get_text(html):
    global content_id
    pattern = '&nbsp;&nbsp;&nbsp;&nbsp;.*<br/><br/>'
    content = "".join(re.findall(pattern, html))
    content = content.replace("&nbsp;", "").replace("<br/>", "\n")
    # 获取第二页内容id
    content_id = get_next_text(html)
    # 如果有第二页
    if content_id:
        # 获取网页响应的内容
        html = get_html(url + book_id[0:5] + "/" + content_id + ".html")
        content += get_text(html)
    return content
# 获取章节id
def get_id_text(html):
    # 数据打包
    title_id_list = []
    pattern = '<li><a href="//m.baode11.cc/ob/'+book_id[0:5]+'/(\d{7}).html"'
    id_lst = re.findall(pattern, html)
    pattern = 'class="xbk">(.*)</a></li>'
    title_lsst = re.findall(pattern, html)
    for a, b in zip(id_lst, title_lsst):
        title_id_list.append([a, b])
    return title_id_list
# 获取下一个章节
def get_id_next(html):
    pattern = '<div class="pagelink"> <a href="//m.baode11.cc/ob/(\d+_\d+)/">下一页</a>'
    return "".join(re.findall(pattern, html))

if __name__ == '__main__':
    while True:
        # 获取响应内容
        html = get_html(url + book_id)
        # 获取全部的章节id
        title_id_list = get_id_text(html)
        # 通过每个章节id下载小说内容
        for title_id in title_id_list:
            # 获取章节标题
            content_id, title = title_id
            # 获取响应内容
            html1 = get_html(url + book_id[0:5] + "/" + content_id + ".html")
            # 获取小说内容
            content = get_text(html1)
            write_file(content, title)
            time.sleep(2)
        break
        # 再获取下一页目录id
        next_id = get_id_next(html)
        if not next_id:
            break
        book_id = next_id + "/"



